package org.example.config;

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
import org.example.store.MongoChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FormAgentConfig {
    @Autowired
    private MongoChatMemoryStore chatMemoryStore;
    @Autowired
    private EmbeddingStore embeddingStore;
    @Autowired
    private EmbeddingModel embeddingModel;

    @Bean
    public ChatMemoryProvider chatMemoryProviderForm() {
        return memoryId-> MessageWindowChatMemory.
                builder()
                .id(memoryId)
                .chatMemoryStore(chatMemoryStore)
                .maxMessages(100).build();
    }



    @Bean
    public EmbeddingStore<TextSegment> embeddingStoreForm() {
        MilvusEmbeddingStore embeddingStore = MilvusEmbeddingStore.builder()
                .host("localhost")      // Docker宿主机IP
                .port(19530)            // 默认端口
                .collectionName("powerform")
                .dimension(embeddingModel.dimension())         // 向量维度需与嵌入模型匹配
                .build();
        return embeddingStore;
    }

    @Bean
    public ContentRetriever contentRetrieverForm() {
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .maxResults(3)
                // .minScore(0.5)
                .build();
    }
}
